﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Instrument
{
    public class SeriPortManage:SerialPort
    {
        private static SeriPortManage _seriPortManage;
        public static SeriPortManage GetInstance()
        {
            if(_seriPortManage == null)
            {
                return new SeriPortManage();
            }
            else
            {
                return _seriPortManage;
            }
        }
        private SeriPortManage()
        {
            Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            this.PortName = configuration.AppSettings.Settings["PortName"].Value;
            this.BaudRate = Convert.ToInt32(configuration.AppSettings.Settings["BaudRate"].Value);
            this.DataBits = Convert.ToInt32(configuration.AppSettings.Settings["DataBits"].Value);
            switch (configuration.AppSettings.Settings["Parity"].Value)
            {
                case "None":
                    this.Parity = Parity.None;
                    break;
                case "Odd":
                    this.Parity = Parity.Odd;
                    break;
                case "Even":
                    this.Parity = Parity.Even;
                    break;
                case "Mark":
                    this.Parity = Parity.Mark;
                    break;
                case "Space":
                    this.Parity = Parity.Space;
                    break;
            }
            switch (configuration.AppSettings.Settings["StopBits"].Value)
            {
                case "None":
                    this.StopBits = StopBits.None;
                    break;
                case "One":
                    this.StopBits = StopBits.One;
                    break;
                case "Two":
                    this.StopBits = StopBits.Two;
                    break;
                case "OnePointFive":
                    this.StopBits = StopBits.OnePointFive;
                    break;
            }
        }
    }
}
